//
//  RightViewController.m
//  Leisure
//
//  Created by xalo on 16/6/28.
//  Copyright © 2016年 Eason. All rights reserved.
//

#import "RightViewController.h"

@interface RightViewController ()

@end

@implementation RightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
// 创建barButton
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"菜单" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction)];
    
//    self.view.backgroundColor = [UIColor blueColor];
    
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(panAction:)];
    [self.navigationController.view addGestureRecognizer:pan];
    
}
- (void)itemAction{
//    向右偏移
    if (!self.isSlider) {
        [self moveRight];
        
    }else{
//        回到原位
        [self backToTheInSitu];
    }
   
    self.isSlider = !self.isSlider;
}

- (void)moveRight{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.view.frame = CGRectMake(kWidth/2, 0, kWidth, kHeight);
        
    }];
}
- (void)backToTheInSitu{
    
    [UIView animateWithDuration:0.2 animations:^{
        self.navigationController.view.frame = CGRectMake(0, 0, kWidth, kHeight);
    }];
}
- (void)panAction:(UIPanGestureRecognizer *)pan{
//    获取到手势移动的点
    CGPoint point = [pan translationInView:self.navigationController.view];
//    设置移动动画
    self.navigationController.view.transform = CGAffineTransformTranslate(self.navigationController.view.transform, point.x, 0);

//    防止跑远
    [pan setTranslation:CGPointZero inView:self.view ];
//    判断手势触摸完毕
    if (pan.state == UIGestureRecognizerStateEnded ) {
        if (self.navigationController.view.frame.origin.x>kWidth/2) {
            [self moveRight];
            self.isSlider = YES;
        }else{
            [self backToTheInSitu];
            self.isSlider = NO;
        }
    }
}

@end
